package com.asset.dispoal.business.appoint.service.command;

import com.asset.dispoal.business.appoint.dto.QueryWithdrawlPageRequest;
import com.asset.dispoal.business.appoint.dto.QueryWithdrawlPageResponse;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import com.asset.dispoal.commom.page.PagingQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

@Component
@Slf4j
public class QueryPageAppointDrawalCommand implements BaseCommand<QueryWithdrawlPageRequest, Result<PageVO<QueryWithdrawlPageResponse>>> {

    @Autowired
    BsCaseMapper bsCaseMapper;


    @Override
    public Result<PageVO<QueryWithdrawlPageResponse>> execute(QueryWithdrawlPageRequest request) {
        String permission = (String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission");
        request.setPermission(permission);
        int count = bsCaseMapper.queryCountWithdrawal(request);
        Page<QueryWithdrawlPageRequest> page = PagingQuery.getPage(count, request);
        PageVO<QueryWithdrawlPageResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<QueryWithdrawlPageResponse> bsCases = bsCaseMapper.queryPageWithdrawal(page);
            pageVO.setResult(bsCases);
        }
        return Result.ok(pageVO);
    }
}
